package com.ujs.pojo;



import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.ujs.utils.DateUtils;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

//订单

@Entity
@Table(name = "tb_order")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class Order {

    @Id
    @Column(name = "order_id")
    @GeneratedValue(generator = "jpa-uuid")
    private String id;//无意义主键

    private String orderNum;//订单编号

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date orderTime;//下单时间

    @Transient
    private String orderTimeStr;//下单时间字符串


    private int peopleCount;//出行人数


    private String orderDesc;//订单描述


    private Integer payType;//支付方式

    @Transient
    private String payTypeStr;


    private int orderStatus;//订单状态

    @Transient
    private String orderStatusStr;

    @ManyToOne(targetEntity = Product.class,fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
//    @JsonIgnoreProperties(value="orders")
    @JsonIgnore
    @JoinColumn(name = "fk_product_id",referencedColumnName = "product_id")
    private Product product;

    @ManyToOne(targetEntity = Member.class,fetch = FetchType.EAGER)
    @JoinColumn(name = "fk_member_id",referencedColumnName = "member_id")
    private Member member;

@ManyToMany(mappedBy = "orders",cascade = CascadeType.REMOVE)
    private Set<Traveller> travellers = new HashSet<>();

    @Override
    public String toString() {
        return "Order{" +
                "id='" + id + '\'' +
                ", orderNum='" + orderNum + '\'' +
                ", orderTime=" + orderTime +
                ", orderTimeStr='" + orderTimeStr + '\'' +
                ", peopleCount=" + peopleCount +
                ", orderDesc='" + orderDesc + '\'' +
                ", payType=" + payType +
                ", payTypeStr='" + payTypeStr + '\'' +
                ", orderStatus=" + orderStatus +
                ", orderStatusStr='" + orderStatusStr + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

    public Date getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    public void setOrderTimeStr(String orderTimeStr) {
        this.orderTimeStr = orderTimeStr;
    }

    public int getPeopleCount() {
        return peopleCount;
    }

    public void setPeopleCount(int peopleCount) {
        this.peopleCount = peopleCount;
    }

    public String getOrderDesc() {
        return orderDesc;
    }

    public void setOrderDesc(String orderDesc) {
        this.orderDesc = orderDesc;
    }

    public Integer getPayType() {
        return payType;
    }

    public void setPayType(Integer payType) {
        this.payType = payType;
    }

    public void setPayTypeStr(String payTypeStr) {
        this.payTypeStr = payTypeStr;
    }

    public int getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus) {
        this.orderStatus = orderStatus;
    }

    public void setOrderStatusStr(String orderStatusStr) {
        this.orderStatusStr = orderStatusStr;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public Set<Traveller> getTravellers() {
        return travellers;
    }

    public void setTravellers(Set<Traveller> travellers) {
        this.travellers = travellers;
    }

    public String getOrderStatusStr() {
        //订单状态(0 未支付 1 已支付)
        if(orderStatus==0){
            orderStatusStr="未支付";
        }else if(orderStatus==1){
            orderStatusStr="已支付";
        }
        return orderStatusStr;
    }


    public String getOrderTimeStr() {
        if(orderTime!=null){
            orderTimeStr= DateUtils.date2String(orderTime,"yyyy-MM-dd HH:mm");
        }
        return orderTimeStr;
    }


    public String getPayTypeStr() {
        //支付方式(0 支付宝 1 微信 2其它)
        if(payType==0){
            payTypeStr="支付宝";
        }else if(payType==1){
            payTypeStr="微信";
        }else if(payType==2){
            payTypeStr="其它";
        }
        return payTypeStr;
    }

}
